package com.bootdo.payee.controller;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.MD5Utils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.payee.domain.PayeeAgentDO;
import com.bootdo.payee.domain.PayeeDO;
import com.bootdo.payee.service.PayeeService;

/**
 * 收款户
 */
@Controller
@RequestMapping("/payee/payee")
public class PayeeController {

    private final String PREFIX = "payee/payee";

    @Autowired
    private PayeeService payeeService;

    @GetMapping()
    @RequiresPermissions("payee:payee:payee")
    String payee(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @PostMapping("/findAgent")
    @RequiresPermissions("payee:payee:payee")
    public R findAgent(){
        List<PayeeAgentDO> list = payeeService.findAgent();
        return R.ok(list);
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("payee:payee:payee")
    PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<PayeeDO> userList = payeeService.list(query);
        int total = payeeService.count(query);
        PageUtils pageUtils = new PageUtils(userList, total);
        return pageUtils;
    }

    @RequiresPermissions("payee:payee:add")
    @Log("添加收款户")
    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("userId", getUserId());
        //获取收款户代理信息
        model.addAttribute("payeeAgentList", payeeService.queryPayeeAgentList());
        return PREFIX + "/add";
    }

    @RequiresPermissions("payee:payee:add")
    @Log("保存收款户")
    @PostMapping("/save")
    @ResponseBody
    R save(PayeeDO payee) {
        payee.setPassword(MD5Utils.encrypt(payee.getUserName(), payee.getPassword()));
        if (payeeService.save(payee) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("payee:payee:edit")
    @Log("编辑收款户")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        PayeeDO payee = payeeService.get(id);
        model.addAttribute("user", payee);
        model.addAttribute("payeeAgentList", payeeService.queryPayeeAgentList());
        return PREFIX + "/edit";
    }

    @RequiresPermissions("payee:payee:edit")
    @Log("修改收款户")
    @PostMapping("/update")
    @ResponseBody
    @IpFilter
    R update(PayeeDO payee) {
        if (payeeService.update(payee) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("payee:payee:editStatus")
    @Log("停用启用收款户")
    @PostMapping("/editStatus")
    @ResponseBody
    R editStatus(Long userId, Integer status) {
        if (payeeService.editStatus(userId, status) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("payee:payee:editLineUpStatus")
    @Log("关闭开启接单状态")
    @PostMapping("/editLineUpStatus")
    @ResponseBody
    R editLineUpStatus(Long userId, Integer status) {
        if (payeeService.editLineUpStatus(userId, status) > 0) {
            return R.ok();
        }
        return R.error(-1, "当前收款户不存在收款账户信息");
    }

    @RequiresPermissions("payee:payee:quota")
    @Log("获取收款户流水")
    @GetMapping("/quota/{id}")
    String quota(Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return "quota/list";
    }

    @RequiresPermissions("payee:payee:device")
    @Log("清除设备")
    @GetMapping("/device/{id}")
    String device(Model model, @PathVariable("id") Long id) {
        model.addAttribute("userId", id);
        return PREFIX + "/device";
    }


    @IpFilter
    @Log("清除设备")
    @ResponseBody
    @PostMapping("/deviceSave")
    @RequiresPermissions("payee:payee:device")
    R deviceSave(Long userId){
        if (payeeService.deviceSave(userId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量禁用/启用
     */
    @PostMapping( "/batchEditStatus")
    @ResponseBody
    @RequiresPermissions("payee:payee:batchEditStatus")
    public R batchEditStatus(@RequestParam("ids[]") Long[] ids){
        if (payeeService.batchEditStatus(ids) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量禁用/启用接单状态
     */
    @PostMapping( "/batchEditLineUpStatus")
    @ResponseBody
    @RequiresPermissions("payee:payee:batchEditLineUpStatus")
    public R batchEditLineUpStatus(@RequestParam("ids[]") Long[] ids){
        if (payeeService.batchEditLineUpStatus(ids) > 0) {
            return R.ok();
        }
        return R.error();
    }

}
